package com.runen.infinitemovement.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.MyMedalItemAdapterV2;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.MedalListEntity;
import com.runen.infinitemovement.model.enums.ActivityCompat;
import com.runen.infinitemovement.tool.AnimationUtils;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class MyMedalListActivity extends BaseActivity {
    @BindView(R.id.rcv_red_long_road)
    RecyclerView rcv_red_long_road;
    @BindView(R.id.rcv_sport_road)
    RecyclerView rcv_sport_road;
    @BindView(R.id.rcv_activity)
    RecyclerView rcv_activity;
    @BindView(R.id.iv_red_long_road)
    ImageView iv_red_long_road;
    @BindView(R.id.iv_sport_road)
    ImageView iv_sport_road;
    @BindView(R.id.iv_activity)
    ImageView iv_activity;
    @BindView(R.id.ll_red_long_road)
    LinearLayout ll_red_long_road;
    @BindView(R.id.ll_sport_road)
    LinearLayout ll_sport_road;
    @BindView(R.id.ll_activity)
    LinearLayout ll_activity;
    @BindView(R.id.tv_red_long_road)
    TextView tv_red_long_road;
    @BindView(R.id.tv_sport_road)
    TextView tv_sport_road;
    @BindView(R.id.tv_activity)
    TextView tv_activity;
    @BindView(R.id.ll_content_1)
    LinearLayout ll_content_1;
    @BindView(R.id.ll_content_2)
    LinearLayout ll_content_2;
    @BindView(R.id.ll_content_3)
    LinearLayout ll_content_3;
    @BindView(R.id.ll_null)
    LinearLayout ll_null;
    @BindView(R.id.tv_user_name)
    TextView tv_user_name;

    private String userId;
    private String userName;

    private MyMedalItemAdapterV2 adapterRedLongRoad;
    private MyMedalItemAdapterV2 adapterSportRoad;
    private MyMedalItemAdapterV2 adapterActivity;

    private boolean isExpanded1 = false;
    private boolean isExpanded2 = false;
    private boolean isExpanded3 = false;

    private MedalListEntity.DataBean data;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_medal_list;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        userId = getIntent().getStringExtra(ActivityCompat.USER_ID);
        userName = getIntent().getStringExtra(ActivityCompat.USER_NAME);
        tv_user_name.setText(userName);
        initData();
    }

    private void initData() {
        String params = "?userId=%s";
        new UserAPI().getUserMedalList(String.format(params, userId), new BaseResultCallback<MedalListEntity>() {
            @Override
            public void onResponse(MedalListEntity response) {
                if (response != null && response.code == 0) {
                    setView(response.data);
                }
            }
        });
    }

    private void setView(MedalListEntity.DataBean data) {
        this.data = data;
        if (data.redLineLongMarchList != null && data.redLineLongMarchList.size() > 0) {
            ll_null.setVisibility(View.GONE);
            ll_content_1.setVisibility(View.VISIBLE);
            LinearLayoutManager layoutManager = new GridLayoutManager(this, 3);
            rcv_red_long_road.setLayoutManager(layoutManager);
            adapterRedLongRoad = new MyMedalItemAdapterV2(this);
            rcv_red_long_road.setAdapter(adapterRedLongRoad);
            List<MedalListEntity.DataBean.ActivityListBean> listBeans = new ArrayList<>();
            listBeans.addAll(data.redLineLongMarchList);
            if (listBeans.size() > 9) {
                ll_red_long_road.setVisibility(View.VISIBLE);
                adapterRedLongRoad.setNewData(listBeans.subList(0, 9));
            } else {
                ll_red_long_road.setVisibility(View.GONE);
                adapterRedLongRoad.setNewData(listBeans);
            }
        } else {
            ll_content_1.setVisibility(View.GONE);
        }

        if (data.sportsJourneyList != null && data.sportsJourneyList.size() > 0) {
            ll_null.setVisibility(View.GONE);
            ll_content_2.setVisibility(View.VISIBLE);
            LinearLayoutManager layoutManager = new GridLayoutManager(this, 3);
            rcv_sport_road.setLayoutManager(layoutManager);
            adapterSportRoad = new MyMedalItemAdapterV2(this);
            rcv_sport_road.setAdapter(adapterSportRoad);
            List<MedalListEntity.DataBean.ActivityListBean> listBeans = new ArrayList<>();
            listBeans.addAll(data.sportsJourneyList);
            if (listBeans.size() > 9) {
                ll_sport_road.setVisibility(View.VISIBLE);
                adapterSportRoad.setNewData(listBeans.subList(0, 9));
            } else {
                ll_sport_road.setVisibility(View.GONE);
                adapterSportRoad.setNewData(listBeans);
            }
        } else {
            ll_content_2.setVisibility(View.GONE);
        }

        if (data.activityList != null && data.activityList.size() > 0) {
            ll_null.setVisibility(View.GONE);
            ll_content_3.setVisibility(View.VISIBLE);
            LinearLayoutManager layoutManager = new GridLayoutManager(this, 3);
            rcv_activity.setLayoutManager(layoutManager);
            adapterActivity = new MyMedalItemAdapterV2(this);
            rcv_activity.setAdapter(adapterActivity);
            List<MedalListEntity.DataBean.ActivityListBean> listBeans = new ArrayList<>();
            listBeans.addAll(data.activityList);
            if (listBeans.size() > 9) {
                ll_activity.setVisibility(View.VISIBLE);
                adapterActivity.setNewData(listBeans.subList(0, 9));
            } else {
                ll_activity.setVisibility(View.GONE);
                adapterActivity.setNewData(listBeans);
            }
        } else {
            ll_content_3.setVisibility(View.GONE);
        }

        if (ll_content_1.getVisibility() == View.GONE &&
                ll_content_2.getVisibility() == View.GONE &&
                ll_content_3.getVisibility() == View.GONE) {
            ll_null.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.btn_finish, R.id.ll_red_long_road, R.id.ll_sport_road, R.id.ll_activity})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_finish:
                finish();
                break;
            case R.id.ll_red_long_road:
                if (!isExpanded1) {
                    AnimationUtils.setRotateAnimation(iv_red_long_road, 0, 180);
                    tv_red_long_road.setText("收起");
                    isExpanded1 = true;
                    List<MedalListEntity.DataBean.ActivityListBean> listBeans = new ArrayList<>();
                    listBeans.addAll(data.redLineLongMarchList.subList(9, data.redLineLongMarchList.size()));
                    adapterRedLongRoad.addData(listBeans);
                } else {
                    AnimationUtils.setRotateAnimation(iv_red_long_road, 180, 360);
                    tv_red_long_road.setText("查看更多");
                    isExpanded1 = false;
                    List<MedalListEntity.DataBean.ActivityListBean> listBeans = new ArrayList<>();
                    listBeans.addAll(data.redLineLongMarchList.subList(0, 9));
                    adapterRedLongRoad.setNewInstance(listBeans);
                }
                break;
            case R.id.ll_sport_road:
                if (!isExpanded2) {
                    AnimationUtils.setRotateAnimation(iv_sport_road, 0, 180);
                    tv_sport_road.setText("收起");
                    isExpanded2 = true;
                    List<MedalListEntity.DataBean.ActivityListBean> listBeans = new ArrayList<>();
                    listBeans.addAll(data.sportsJourneyList.subList(9, data.sportsJourneyList.size()));
                    adapterSportRoad.addData(listBeans);
                } else {
                    AnimationUtils.setRotateAnimation(iv_sport_road, 180, 360);
                    tv_sport_road.setText("查看更多");
                    isExpanded2 = false;
                    List<MedalListEntity.DataBean.ActivityListBean> listBeans = new ArrayList<>();
                    listBeans.addAll(data.sportsJourneyList.subList(0, 9));
                    adapterSportRoad.setNewInstance(listBeans);
                }
                break;
            case R.id.ll_activity:
                if (!isExpanded3) {
                    AnimationUtils.setRotateAnimation(iv_activity, 0, 180);
                    tv_activity.setText("收起");
                    isExpanded3 = true;
                    List<MedalListEntity.DataBean.ActivityListBean> listBeans = new ArrayList<>();
                    listBeans.addAll(data.activityList.subList(9, data.activityList.size()));
                    adapterActivity.addData(listBeans);
                } else {
                    AnimationUtils.setRotateAnimation(iv_activity, 180, 360);
                    tv_activity.setText("查看更多");
                    isExpanded3 = false;
                    List<MedalListEntity.DataBean.ActivityListBean> listBeans = new ArrayList<>();
                    listBeans.addAll(data.activityList.subList(0, 9));
                    adapterActivity.setNewInstance(listBeans);
                }
                break;
        }
    }
}
